package com.catmiao.ai.controller;

import com.catmiao.ai.assistant.ChatAssistant;
import com.catmiao.ai.prompt.LawAssistant;
import com.catmiao.ai.prompt.LawPrompt;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
public class PromptController {


    @Resource
    private LawAssistant lawAssistant;

    @Resource
    @Qualifier("qwen")
    private ChatModel chatModelQwen;



    @GetMapping("/prompt/chat")
    public Flux<String> chat(String prompt){
        System.out.println("123");
        return lawAssistant.chatStream(prompt,100);
    }

    @GetMapping("/prompt/chat2")
    public Flux<String> chat2(){
        LawPrompt lawPrompt = new LawPrompt();
        lawPrompt.setLegal("劳动法");
        lawPrompt.setQuestion("关于加班有哪些规定");
        return lawAssistant.chatStream(lawPrompt);
    }

    @GetMapping("/prompt/chat3")
    public String chat3(){

        String role = "外科医生";
        String question = "牙疼";

        // 1. 构造 promptTemplate模板
        PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手，请回答：{{question}}怎么办");
        // 2. 由template构造prompt
        Prompt prompt = template.apply(Map.of("it", role, "question", question));
        // 3. Prompt提示词变成UserMessage
        UserMessage userMessage = prompt.toUserMessage();

        ChatResponse response = chatModelQwen.chat(userMessage);
        String message = response.aiMessage().text();

        System.out.println(message);
        return message;
    }
}
